package com.hs.web;

import com.hs.service.UserService;
import com.hs.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、设置编码格式
//        req.setCharacterEncoding("UTF-8");
//        resp.setCharacterEncoding("UTF-8");
//        resp.setContentType("text/html;charset=utf-8");
        //获取请求的信息
       String username= req.getParameter("username");
        String password = req.getParameter("password");
        //引入业务逻辑接口处理请求信息
        UserService userService=new UserServiceImpl();
        int code=userService.login(username,password);
        String mess="";
        //给你响应内容
        // 0代表登录成功，1 用户错了 2 密码不对
        if(code == 0){
            //响应重定向
            resp.sendRedirect("/teacherCount");
        }else if (code ==1){
            req.setAttribute("mess","用户名不存在！！");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }else if(code ==2){
            req.setAttribute("mess","密码错误，请重新输入！！");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req,resp);
    }
}
